var _n62;
var cc__extends = __extends;
var cc__decorate = __decorate;
var cc__awaiter = __awaiter;
var cc__generator = __generator;
var cc__importDefault = __importDefault;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var cc_ResourceLoader = require("ResourceLoader");
var zqcc_resourceLoader = cc__importDefault(cc_ResourceLoader);
var cc_yyComponent = require("yyComponent");
var zqcc_yyComponent = cc__importDefault(cc_yyComponent);
var cc_gameMode_Json = require("gameMode_Json");
var cc_userService = require("userService");
var zqcc_userService = cc__importDefault(cc_userService);
var cc_OverallSystem = require("zqcc_OverallSystem");
var cc_PaintBoard = require("zqcc_PaintBoard");
var zqcc_PaintBoard = cc__importDefault(cc_PaintBoard);
var cc_cc__decorator = cc._decorator;
var cc_ccp_ccclass = cc_cc__decorator.ccclass;
var cc_ccp_property = cc_cc__decorator.property;
var def_zqcc_home = function (t) {
  function _ctor() {
    var instance = (t !== null) ? t.apply(this, arguments) : this;

    // Initialize properties for the instance
    instance.zqcc_paintBoard = null;
    instance.zqcc_tianjiazhuomianBtn = null;
    instance.zqcc_rankNode = null;
    instance.zqcc_cebianlanNode = null;
    instance.zqcc_loadingOver = false;

    // Return the initialized instance
    return instance;
  }
  cc__extends(_ctor, t);
  _ctor.prototype.onLoad = function () { };
  _ctor.prototype.start = function () {
    this.node.getComponent(cc.Canvas).fitHeight = zqcc_userService.default.IPAD;
    this.node.getComponent(cc.Canvas).fitWidth = !zqcc_userService.default.IPAD;

    // Check if the app is running on ByteDance's platform
    if (window.tt) {
      var launchOptions = window.tt.getLaunchOptionsSync();

      // Verify scene and check if it meets specific criteria
      if (launchOptions.scene !== "101001" || !cc_OverallSystem.sers().userSrv.getCheckScene()) {
        window.sceneCode = launchOptions.scene;
        cc_OverallSystem.sers().uiSrv.showPage("cebianlan", "cebianlan");
      }
    }

    // Play background music
    cc_OverallSystem.mgrs().soundMgr.playMusic("bgm");

    // Control the visibility of the 'zqcc_cebianlanNode' based on the platform
    this.zqcc_cebianlanNode.active = (cc.sys.platform === cc.sys.BYTEDANCE_GAME);

    // Execute additional checks
    this.zqcc_showCheck();

    // Retrieve user data and initialize rank and painting settings
    var userInfo = cc_OverallSystem.sers().userSrv.getFeibiaoData();
    this.zqcc_setImRankData(userInfo.level);
    this.zqcc_initPaintBoard(cc_OverallSystem.sers().userSrv.getSelectedPainting());

    // Record the end of the game screen
    cc_OverallSystem.sers().platformSrv.platformApi.recordedGameScreenStop();

    // Set up an event listener for painting changes
    cc.game.on("changePainting", this.zqcc_initPaintBoard, this);
  };
  _ctor.prototype.zqcc_initPaintBoard = function (t) {
    return cc__awaiter(this, undefined, undefined, function () {
      var result;
      return cc__generator(this, function (context) {
        switch (context.label) {
          case 0:
            // Load the JSON asset for the specified level from the 'Feibiao' bundle
            return [4, zqcc_resourceLoader.default.getInstance().load("Feibiao", "rope/json/lv_" + t, cc.JsonAsset)];

          case 1:
            // Get the loaded JSON data
            result = context.sent();
            cc.log("+++++++", result.json);

            // Load the grid data into the zqcc_paintBoard and jump to the last step
            this.zqcc_paintBoard.zqcc_loadGridData(result.json);
            this.zqcc_paintBoard.zqcc_jumpToLastStep();
            return [2];
        }
      });
    });
  };
  _ctor.prototype.zqcc_setBtn = function () {
    cc_OverallSystem.mgrs().soundMgr.playBtnSound();
    cc_OverallSystem.sers().uiSrv.showSettingPage();
  };
  _ctor.prototype.zqcc_openGengDuoWanfa = function () {
    cc_OverallSystem.mgrs().soundMgr.playBtnSound();
    cc_OverallSystem.sers().uiSrv.showGameListNewPage();
  };
  _ctor.prototype.zqcc_showPifuPage = function () {
    cc_OverallSystem.mgrs().soundMgr.playBtnSound();
    cc_OverallSystem.sers().uiSrv.showPifuPage();
  };
  _ctor.prototype.zqcc_startGame = function () {
    cc_OverallSystem.mgrs().soundMgr.playBtnSound();
    cc_OverallSystem.mgrs().gameMgr.enterMode(cc_gameMode_Json.GAME_MODE.Feibiao, true);
  };
  _ctor.prototype.zqcc_qiandao = function () {
    window.qiandao = true;
    cc_OverallSystem.mgrs().soundMgr.playBtnSound();
    cc_OverallSystem.sers().uiSrv.showSignInPage();
  };
  _ctor.prototype.zqcc_zhuti = function () {
    cc_OverallSystem.mgrs().soundMgr.playBtnSound();
    cc_OverallSystem.sers().uiSrv.showZhutiListPage();
  };
  _ctor.prototype.zqcc_onPintu = function () {
    cc_OverallSystem.mgrs().soundMgr.playBtnSound();
    cc_OverallSystem.sers().uiSrv.showPage("Collection", "CollectionPage");
  };
  _ctor.prototype.zqcc_showRank = function () {
    // Play button sound
    cc_OverallSystem.mgrs().soundMgr.playBtnSound();

    if (window.tt) {
      var context = this;

      // Call the login method for the ByteDance platform
      window.tt.login({
        force: true,
        success(response) {
          console.log("Login successful: " + response.code + " " + response.anonymousCode);
          context.zqcc_getImRankList(); // Retrieve the ranking list after a successful login
        },
        fail() {
          console.log("Login failed");
        }
      });
    } else if (window.wx) {
      // Show the ranking list page for WeChat
      cc_OverallSystem.sers().uiSrv.showRankingListPage();
    } else {
      // Check user data for city information
      var city = cc_OverallSystem.sers().userSrv.getUserData().city;

      if (city !== null && city !== "") {
        // If city is present, show the ranking list page
        cc_OverallSystem.sers().uiSrv.showRankingListPage();
      } else {
        // If city is not available, prompt user to select a region
        var dialogOptions = {
          continueCall() {
            cc_OverallSystem.sers().uiSrv.showRankingListPage(); // Proceed to ranking list page
          },
          closeCall() {
            // Optional close logic
          }
        };
        cc_OverallSystem.sers().uiSrv.showSelectDiQuPage(dialogOptions); // Show selection page
      }
    }
  };
  _ctor.prototype.zqcc_setImRankData = function (t) {
    // Set the visibility of the zqcc_rankNode based on the presence of the WeChat platform
    this.zqcc_rankNode.active = !window.wx;
    cc.log("Window object:", window.wx);

    // Check if the ByteDance platform is present and the KingSoft platform is not
    if (window.tt && !window.ks) {
      // Set ranking data for the ByteDance platform
      window.tt.setImRankData({
        dataType: 0,    // Define data type
        value: t + "",  // Convert value to string
        priority: 0,    // Set priority level
        extra: "extra", // Additional information
        zoneId: "default", // Default zone ID
        success(response) {
          console.log("zqcc_setImRankData success response: " + response);
        },
        fail(error) {
          console.log("zqcc_setImRankData failed: " + error.errMsg);
        }
      });
    }
  };
  _ctor.prototype.zqcc_getImRankList = function () {
    // Fetch the ranking list from the ByteDance platform
    window.tt.getImRankList({
      relationType: "default", // Default relationship type
      dataType: 0,             // Specify the type of data to retrieve
      rankType: "all",        // Request ranking data for all types
      suffix: "",              // Suffix for the ranking data, if any
      rankTitle: "排行榜",      // Title for the ranking list
      zoneId: "default",       // Zone identifier for the ranking

      // Success callback
      success(response) {
        console.log("getImRankData success response: " + response);
      },

      // Failure callback
      fail(error) {
        console.log("getImRankData failed: " + error.errMsg);
      }
    });
  };
  _ctor.prototype.zqcc_share = function () {
    cc_OverallSystem.mgrs().soundMgr.playBtnSound();
    cc_OverallSystem.sers().uiSrv.showPage("Invite", "InvitePage");
  };
  _ctor.prototype.zqcc_showCheck = function () {
    // If the KingSoft platform is available, assign the ByteDance reference
    if (window.ks) {
      window.tt = window.ks;
    }

    // Check for the existence of the shortcut if the ByteDance platform is available
    if (window.tt && window.tt.checkShortcut) {
      var context = this; // Reference to the current context

      window.tt.checkShortcut({
        // Success callback for checking the shortcut
        success(response) {
          console.log("Checking shortcut...");
          console.log("Shortcut existence status:", response.status);

          // Update button visibility based on shortcut existence
          context.zqcc_tianjiazhuomianBtn.active = !response.status.exist;

          // Retrieve system information
          window.tt.getSystemInfo({
            // Success callback for system info
            success(info) {
              console.log("appName: " + info.appName);

              // Hide the button based on app name and platform
              if (["Tt_lite", "Toutiao", "xigua"].includes(info.appName)) {
                context.zqcc_tianjiazhuomianBtn.active = false;
              }
              if (info.platform === "ios") {
                context.zqcc_tianjiazhuomianBtn.active = true;
              }
            },
            // Fail callback for system info
            fail() {
              console.log("Failed to retrieve system info");
            }
          });
        },
        // Failure callback for checking the shortcut
        fail(error) {
          console.log("Shortcut check failed:", error.errMsg);
          context.zqcc_tianjiazhuomianBtn.active = true; // Enable button on failure
        }
      });
    } else {
      // Disable the button if the ByteDance platform is not available
      this.zqcc_tianjiazhuomianBtn.active = false;
    }
  };
  _ctor.prototype.zqcc_tianjiadaozhuomian = function () {
    // Play the button sound
    cc_OverallSystem.mgrs().soundMgr.playBtnSound();

    // Check if the ByteDance platform is available
    if (window.tt) {
      // Try to add a shortcut for the ByteDance platform
      window.tt.addShortcut({
        success() {
          cc.log("Shortcut added successfully");
          cc_OverallSystem.sers().uiSrv.showMessage("添加桌面成功"); // Show success message
        },
        fail(error) {
          cc.log("Failed to add shortcut:", error.errMsg);
          cc_OverallSystem.sers().uiSrv.showMessage("添加桌面失败"); // Show error message
        }
      });
    } else if (window.qg) {
      // Handle shortcut creation for the QG platform
      var context = this; // Capture the current context

      window.qg.installShortcut({
        success() {
          console.log("Shortcut created successfully");
          context.zqcc_tianjiazhuomianBtn.active = false; // Disable button after successful creation
        }
      });
    }
  };
  _ctor.prototype.zqcc_onMoreGame = function () {
    cc_OverallSystem.mgrs().soundMgr.playBtnSound();
    cc_OverallSystem.sers().uiSrv.showMessage("敬请期待");
  };
  cc__decorate([cc_ccp_property(zqcc_PaintBoard.default)], _ctor.prototype, "zqcc_paintBoard", undefined);
  cc__decorate([cc_ccp_property(cc.Node)], _ctor.prototype, "zqcc_tianjiazhuomianBtn", undefined);
  cc__decorate([cc_ccp_property(cc.Node)], _ctor.prototype, "zqcc_rankNode", undefined);
  cc__decorate([cc_ccp_property(cc.Node)], _ctor.prototype, "zqcc_cebianlanNode", undefined);
  return cc__decorate([cc_ccp_ccclass], _ctor);
}(zqcc_yyComponent.default);
exports.default = def_zqcc_home;